package com.ly.mp.busicen.common.context;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.ly.mp.busicen.common.constant.UserBusiEntity;
import com.ly.mp.busicen.common.helper.StringHelper;
import com.ly.mp.component.entities.UserEntity;

public class BusicenUtils {
    public static saveOrUpdate Save = saveOrUpdate.Save;
    public static saveOrUpdate Update = saveOrUpdate.Update;

    /**
     * 
     * @Description: Ipage类型转换为ListResult ,供分页查询使用
     *
     * @version: v1.0.0
     * @author: ly-wyifan
     * @date: 2019年7月3日 下午8:59:31
     *
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------* 2019年7月3日
     *        ly-wyifan v1.0.0 修改原因
     */
    @Deprecated
    public static <T> ListResult<T> page2ListResult(IPage<T> page, int pageIndex, int pageSize) {
        ListResult<T> result = new ListResult<T>();
        result.setRows(page.getRecords());
        result.setRecords((int) page.getTotal());
        result.setPages((int) page.getPages());
        result.setPageindex((int) page.getCurrent());
        result.setResult("1");
        result.setMsg("操作成功");
        return result;
    }

    /**
     * 
     * @Description: 更新Ipage类型转换为ListResult ,供分页查询使用
     *
     * @version: v1.0.0
     * @author: ly-wyifan
     * @date: 2019年7月9日 下午9:13:20
     *
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------* 2019年7月9日
     *        ly-wyifan v1.0.0 修改原因
     */
    public static <T> ListResult<T> page2ListResult(IPage<T> page) {
        ListResult<T> result = new ListResult<>();
        result.setRows(page.getRecords());
        result.setRecords((int) page.getTotal());
        result.setPages((int) page.getPages());
        result.setPageindex((int) page.getCurrent());
        result.setResult("1");
        result.setMsg("查询成功");
        return result;
    }

    /**
     * 
     * @Description:返回ListResult<T> （ 自用到时会删掉，其他人请不要调用）
     *
     * @version: v1.0.0
     * @author: ly-chenjunl
     * @date: 2019年7月16日 上午11:05:01
     */
    @Deprecated
    public static <T> ListResult<T> getQueryList(Class<T> clazz) {
        ListResult<T> result = new ListResult<T>();
        List<T> rcList = new ArrayList<T>();
        T model = null;
        try {
            model = clazz.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
        }
        rcList.add(model);
        result.setRows(rcList);
        result.setRecords(1);
        result.setPages(1);
        result.setPageindex(1);
        result.setResult("1");
        result.setMsg("sucess");
        return result;
    }

    /**
     * 
     * @Description: 用于替换频繁的set方法。包括set
     *               oemId,oemCode,groupId,groupCode,modifier,lastUpdateDate
     *
     * @version: v1.0.0
     * @author: ly-wyifan
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @date: 2019年7月25日 上午11:08:09
     *
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------* 2019年7月10日
     *        ly-wyifan v1.0.0 修改原因 使用枚举类型
     */
    @SuppressWarnings("unchecked")
    public static <T> T invokeUserInfo(T object, saveOrUpdate enums) {
        if (object == null) {
            return null;
        }
        UserEntity userEntity = BusicenContext.getCurrentUser();
        UserBusiEntity userBusiEntity = BusicenContext.getCurrentUserBusiInfo();
        if(object instanceof Map) {
            @SuppressWarnings("rawtypes")
            Map map = (Map)object;
            // 保存
            if (enums.equals(Save)) {
                if(userEntity!=null)
                {
                    // oemId
                    map.put("oemId", userBusiEntity.getOemID());
                    // oemCode
                    map.put("oemCode", userBusiEntity.getOemCode());
                    // groupId
                    map.put("groupId", userBusiEntity.getGroupID());
                    // groupCode
                    map.put("groupCode", userBusiEntity.getGroupCode());
                    // 创建人名称
                    map.put("createdName", userBusiEntity.getEmpName());
                    // 修改人名称
                    map.put("modifyName", userBusiEntity.getEmpName());
                    // 最後修改人 modifier
                    map.put("modifier", userBusiEntity.getUserID());
                    // 创建人
                    map.put("creator", userBusiEntity.getUserID());
                }
                else {
                    // oemId
                    map.put("oemId", "1");
                    // oemCode
                    map.put("oemCode","1");
                    // groupId
                    map.put("groupId", "1");
                    // groupCode
                    map.put("groupCode", "1");
                    // 创建人名称
                    map.put("createdName", "sys");
                    // 修改人名称
                    map.put("modifyName", "sys");
                    // 最後修改人 modifier
                    map.put("modifier","1");
                    // 创建人
                    map.put("creator", "1");
                }
                // 并发控制Id updateDateControlId
                map.put("updateControlId", IdWorker.get32UUID());
                // 创建时间 createdDate
                map.put("createdDate", LocalDateTime.now());
                // 最後更新時間 lastUpdateDate
                map.put("lastUpdatedDate", LocalDateTime.now());
            } else {
                // 修改
                // 最後更新時間 lastUpdateDate
                map.put("lastUpdatedDate", LocalDateTime.now());
                if(userEntity!=null)
                {
                    // 最後修改人 modifier
                    map.put("modifier", userBusiEntity.getUserID());
                    // 最後修改人名称 modifierName
                    map.put("modifyName", userBusiEntity.getEmpName());
                }else {
                    // 最後修改人 modifier
                    map.put("modifier","1");
                    // 最後修改人名称 modifierName
                    map.put("modifyName", "sys");
                }
                // 并发控制Id updateDateControlId
                map.put("updateControlId", IdWorker.get32UUID());
            }
            return object;
        }
        Class<T> clazz = (Class<T>) object.getClass();
        try {
            // 保存
            if(userEntity!=null)
            {
                switch (enums) {
                case Save:
                    // oemId
                    Method method = clazz.getMethod("setOemId", String.class);
                    method.invoke(object, userBusiEntity.getOemID());
                    // oemCode
                    Method method1 = clazz.getMethod("setOemCode", String.class);
                    method1.invoke(object, userBusiEntity.getOemCode());
                    // groupId
                    Method method2 = clazz.getMethod("setGroupId", String.class);
                    method2.invoke(object, userBusiEntity.getGroupID());
                    // groupCode
                    Method method3 = clazz.getMethod("setGroupCode", String.class);
                    method3.invoke(object, userBusiEntity.getGroupCode());
                    // 创建人名称
                    Method method4 = clazz.getMethod("setCreatedName", String.class);
                    method4.invoke(object, userBusiEntity.getEmpName());
                    // 修改人名称
                    Method method5 = clazz.getMethod("setModifyName", String.class);
                    method5.invoke(object, userBusiEntity.getEmpName());
                    // 最後修改人 modifier
                    Method method6 = clazz.getMethod("setModifier", String.class);
                    method6.invoke(object, userEntity.getUserID());
                    // 创建人
                    Method method7 = clazz.getMethod("setCreator", String.class);
                    method7.invoke(object, userEntity.getUserID());
                    // 并发控制Id updateDateControlId
                    Method method8 = clazz.getMethod("setUpdateControlId", String.class);
                    method8.invoke(object, IdWorker.get32UUID());
                    // 创建时间 createdDate
                    Method method9 = clazz.getMethod("setCreatedDate", LocalDateTime.class);
                    method9.invoke(object, LocalDateTime.now());
                    // 最後更新時間 lastUpdateDate
                    Method method10 = clazz.getMethod("setLastUpdatedDate", LocalDateTime.class);
                    method10.invoke(object, LocalDateTime.now());
                    break;
                case Update:
                    // 修改
                    // 最後更新時間 lastUpdateDate
                    Method method11 = clazz.getMethod("setLastUpdatedDate", LocalDateTime.class);
                    method11.invoke(object, LocalDateTime.now());
                    // 最後修改人 modifier
                    Method method13 = clazz.getMethod("setModifier", String.class);
                    method13.invoke(object, userEntity.getUserID());
                    // 最後修改人名称 modifierName
                    Method method12 = clazz.getMethod("setModifyName", String.class);
                    method12.invoke(object, userEntity.getEmpName());
                    // 并发控制Id updateDateControlId
                    Method method17 = clazz.getMethod("setUpdateControlId", String.class);
                    method17.invoke(object, IdWorker.get32UUID());
                default:
                    break;
                }
            }
            else 
            {
                switch (enums) {
                case Save:
                    // oemId
                    Method method = clazz.getMethod("setOemId", String.class);
                    method.invoke(object, "1");
                    // oemCode
                    Method method1 = clazz.getMethod("setOemCode", String.class);
                    method1.invoke(object, "1");
                    // groupId
                    Method method2 = clazz.getMethod("setGroupId", String.class);
                    method2.invoke(object, "1");
                    // groupCode
                    Method method3 = clazz.getMethod("setGroupCode", String.class);
                    method3.invoke(object,"1");
                    // 创建人名称
                    Method method4 = clazz.getMethod("setCreatedName", String.class);
                    method4.invoke(object, "sys");
                    // 修改人名称
                    Method method5 = clazz.getMethod("setModifyName", String.class);
                    method5.invoke(object, "sys");
                    // 最後修改人 modifier
                    Method method6 = clazz.getMethod("setModifier", String.class);
                    method6.invoke(object, "1");
                    // 创建人
                    Method method7 = clazz.getMethod("setCreator", String.class);
                    method7.invoke(object, "1");
                    // 并发控制Id updateDateControlId
                    Method method8 = clazz.getMethod("setUpdateControlId", String.class);
                    method8.invoke(object, IdWorker.get32UUID());
                    // 创建时间 createdDate
                    Method method9 = clazz.getMethod("setCreatedDate", LocalDateTime.class);
                    method9.invoke(object, LocalDateTime.now());
                    // 最後更新時間 lastUpdateDate
                    Method method10 = clazz.getMethod("setLastUpdatedDate", LocalDateTime.class);
                    method10.invoke(object, LocalDateTime.now());
                    break;
                case Update:
                    // 修改
                    // 最後更新時間 lastUpdateDate
                    Method method11 = clazz.getMethod("setLastUpdatedDate", LocalDateTime.class);
                    method11.invoke(object, LocalDateTime.now());
                    // 最後修改人 modifier
                    Method method13 = clazz.getMethod("setModifier", String.class);
                    method13.invoke(object, "1");
                    // 最後修改人名称 modifierName
                    Method method12 = clazz.getMethod("setModifyName", String.class);
                    method12.invoke(object, "sys");
                    // 并发控制Id updateDateControlId
                    Method method17 = clazz.getMethod("setUpdateControlId", String.class);
                    method17.invoke(object, IdWorker.get32UUID());
                default:
                    break;
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return object;
    }
    
    /**
     * 
     * @Description: 用于替换频繁的set方法。包括set
     *               oemId,oemCode,groupId,groupCode,modifier,lastUpdateDate
     *
     * @version: v1.0.0
     * @author: ly-wyifan
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @date: 2019年7月25日 上午11:08:09
     *
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------* 2019年7月10日
     *        ly-wyifan v1.0.0 修改原因 使用枚举类型
     *        添加操作人用户参数，为空则取token里的用户
     */
    @SuppressWarnings("unchecked")
    public static <T> T invokeUserInfo(T object, saveOrUpdate enums,UserBusiEntity user) {
        if (object == null) {
            return null;
        }
        //UserEntity userEntity = BusicenContext.getCurrentUser();
        UserBusiEntity userBusiEntity = BusicenContext.getCurrentUserBusiInfo();
        if(userBusiEntity == null || StringUtils.isBlank(userBusiEntity.getUserID())){
            userBusiEntity=user;
        }
        if(object instanceof Map) {
            @SuppressWarnings("rawtypes")
            Map map = (Map)object;
            // 保存
            if (enums.equals(Save)) {
                // oemId
                map.put("oemId", userBusiEntity.getOemID());
                // oemCode
                map.put("oemCode", userBusiEntity.getOemCode());
                // groupId
                map.put("groupId", userBusiEntity.getGroupID());
                // groupCode
                map.put("groupCode", userBusiEntity.getGroupCode());
                // 创建人名称
                map.put("createdName", userBusiEntity.getEmpName());
                // 修改人名称
                map.put("modifyName", userBusiEntity.getEmpName());
                // 最後修改人 modifier
                map.put("modifier", userBusiEntity.getUserID());
                // 创建人
                map.put("creator", userBusiEntity.getUserID());
                // 并发控制Id updateDateControlId
                map.put("updateControlId", IdWorker.get32UUID());
                // 创建时间 createdDate
                map.put("createdDate", LocalDateTime.now());
                // 最後更新時間 lastUpdateDate
                map.put("lastUpdatedDate", LocalDateTime.now());
            } else {
                // 修改
                // 最後更新時間 lastUpdateDate
                map.put("lastUpdatedDate", LocalDateTime.now());
                // 最後修改人 modifier
                map.put("modifier", userBusiEntity.getUserID());
                // 最後修改人名称 modifierName
                map.put("modifyName", userBusiEntity.getEmpName());
                // 并发控制Id updateDateControlId
                map.put("updateControlId", IdWorker.get32UUID());
            }
            return object;
        }
        Class<T> clazz = (Class<T>) object.getClass();
        try {
            // 保存
            switch (enums) {
            case Save:
                // oemId
                Method method = clazz.getMethod("setOemId", String.class);
                method.invoke(object, userBusiEntity.getOemID());
                // oemCode
                Method method1 = clazz.getMethod("setOemCode", String.class);
                method1.invoke(object, userBusiEntity.getOemCode());
                // groupId
                Method method2 = clazz.getMethod("setGroupId", String.class);
                method2.invoke(object, userBusiEntity.getGroupID());
                // groupCode
                Method method3 = clazz.getMethod("setGroupCode", String.class);
                method3.invoke(object, userBusiEntity.getGroupCode());
                // 创建人名称
                Method method4 = clazz.getMethod("setCreatedName", String.class);
                method4.invoke(object, userBusiEntity.getEmpName());
                // 修改人名称
                Method method5 = clazz.getMethod("setModifyName", String.class);
                method5.invoke(object, userBusiEntity.getEmpName());
                // 最後修改人 modifier
                Method method6 = clazz.getMethod("setModifier", String.class);
                method6.invoke(object, userBusiEntity.getUserID());
                // 创建人
                Method method7 = clazz.getMethod("setCreator", String.class);
                method7.invoke(object, userBusiEntity.getUserID());
                // 并发控制Id updateDateControlId
                Method method8 = clazz.getMethod("setUpdateControlId", String.class);
                method8.invoke(object, IdWorker.get32UUID());
                // 创建时间 createdDate
                Method method9 = clazz.getMethod("setCreatedDate", LocalDateTime.class);
                method9.invoke(object, LocalDateTime.now());
                // 最後更新時間 lastUpdateDate
                Method method10 = clazz.getMethod("setLastUpdatedDate", LocalDateTime.class);
                method10.invoke(object, LocalDateTime.now());
                break;
            case Update:
                // 修改
                // 最後更新時間 lastUpdateDate
                Method method11 = clazz.getMethod("setLastUpdatedDate", LocalDateTime.class);
                method11.invoke(object, LocalDateTime.now());
                // 最後修改人 modifier
                Method method13 = clazz.getMethod("setModifier", String.class);
                method13.invoke(object, userBusiEntity.getUserID());
                // 最後修改人名称 modifierName
                Method method12 = clazz.getMethod("setModifyName", String.class);
                method12.invoke(object, userBusiEntity.getEmpName());
                // 并发控制Id updateDateControlId
                Method method17 = clazz.getMethod("setUpdateControlId", String.class);
                method17.invoke(object, IdWorker.get32UUID());
            default:
                break;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return object;
    }

    @SuppressWarnings("unchecked")
    public static <T> T invokeUserInfo(T object, SOU enums) {
        if (object == null) {
            return null;
        }
        UserEntity userEntity = BusicenContext.getCurrentUser();
        UserBusiEntity userBusiEntity = BusicenContext.getCurrentUserBusiInfo();
        if(object instanceof Map) {
            @SuppressWarnings("rawtypes")
            Map map = (Map)object;
            // 保存
            if (enums.equals(SOU.Save)) {
                // oemId
                map.put("oemId", userBusiEntity.getOemID());
                // oemCode
                map.put("oemCode", userBusiEntity.getOemCode());
                // groupId
                map.put("groupId", userBusiEntity.getGroupID());
                // groupCode
                map.put("groupCode", userBusiEntity.getGroupCode());
                // 创建人名称
                map.put("createdName", userBusiEntity.getEmpName());
                // 修改人名称
                map.put("modifyName", userBusiEntity.getEmpName());
                // 最後修改人 modifier
                map.put("modifier", userBusiEntity.getUserID());
                // 创建人
                map.put("creator", userBusiEntity.getUserID());
                // 并发控制Id updateDateControlId
                map.put("updateControlId", IdWorker.get32UUID());
                // 创建时间 createdDate
                map.put("createdDate", LocalDateTime.now());
                // 最後更新時間 lastUpdateDate
                map.put("lastUpdatedDate", LocalDateTime.now());
            } else {
                // 修改
                // 最後更新時間 lastUpdateDate
                map.put("lastUpdatedDate", LocalDateTime.now());
                // 最後修改人 modifier
                map.put("modifier", userBusiEntity.getUserID());
                // 最後修改人名称 modifierName
                map.put("modifyName", userBusiEntity.getEmpName());
                // 并发控制Id updateDateControlId
                map.put("updateControlId", IdWorker.get32UUID());
            }
            return object;
        }
        Class<T> clazz = (Class<T>) object.getClass();
        try {
            // 保存
            switch (enums) {
            case Save:
                // oemId
                Method method = clazz.getMethod("setOemId", String.class);
                method.invoke(object, userBusiEntity.getOemID());
                // oemCode
                Method method1 = clazz.getMethod("setOemCode", String.class);
                method1.invoke(object, userBusiEntity.getOemCode());
                // groupId
                Method method2 = clazz.getMethod("setGroupId", String.class);
                method2.invoke(object, userBusiEntity.getGroupID());
                // groupCode
                Method method3 = clazz.getMethod("setGroupCode", String.class);
                method3.invoke(object, userBusiEntity.getGroupCode());
                // 创建人名称
                Method method4 = clazz.getMethod("setCreatedName", String.class);
                method4.invoke(object, userBusiEntity.getEmpName());
                // 修改人名称
                Method method5 = clazz.getMethod("setModifyName", String.class);
                method5.invoke(object, userBusiEntity.getEmpName());
                // 最後修改人 modifier
                Method method6 = clazz.getMethod("setModifier", String.class);
                method6.invoke(object, userEntity.getUserID());
                // 创建人
                Method method7 = clazz.getMethod("setCreator", String.class);
                method7.invoke(object, userEntity.getUserID());
                // 并发控制Id updateDateControlId
                Method method8 = clazz.getMethod("setUpdateControlId", String.class);
                method8.invoke(object, IdWorker.get32UUID());
                // 创建时间 createdDate
                Method method9 = clazz.getMethod("setCreatedDate", LocalDateTime.class);
                method9.invoke(object, LocalDateTime.now());
                // 最後更新時間 lastUpdateDate
                Method method10 = clazz.getMethod("setLastUpdatedDate", LocalDateTime.class);
                method10.invoke(object, LocalDateTime.now());
                break;
            case Update:
                // 修改
                // 最後更新時間 lastUpdateDate
                Method method11 = clazz.getMethod("setLastUpdatedDate", LocalDateTime.class);
                method11.invoke(object, LocalDateTime.now());
                // 最後修改人 modifier
                Method method13 = clazz.getMethod("setModifier", String.class);
                method13.invoke(object, userEntity.getUserID());
                // 最後修改人名称 modifierName
                Method method12 = clazz.getMethod("setModifyName", String.class);
                method12.invoke(object, userEntity.getEmpName());
                // 并发控制Id updateDateControlId
                Method method17 = clazz.getMethod("setUpdateControlId", String.class);
                method17.invoke(object, IdWorker.get32UUID());
            default:
                break;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return object;
    }

    public enum saveOrUpdate {
        Save, Update
    }

    public enum SOU {
        Save, Update
    }

    /**
     * 
     * @Description: 用于替换频繁的set方法。包括set
     *               oemId,oemCode,groupId,groupCode,modifier,lastUpdateDate
     *
     * @version: v1.0.0
     * @author: ly-wyifan
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @date: 2019年7月10日 上午10:00:09
     *
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------* 2019年7月10日
     *        ly-wyifan v1.0.0 修改原因
     */
    @SuppressWarnings("unchecked")
    @Deprecated
    public static <T> T invokeUserInfo(T object, int isSave) {
        if (object == null) {
            return null;
        }
        UserEntity userEntity = BusicenContext.getCurrentUser();
        UserBusiEntity userBusiEntity = BusicenContext.getCurrentUserBusiInfo();
        if(object instanceof Map) {
            @SuppressWarnings("rawtypes")
            Map map = (Map)object;
            // 保存
            if (isSave == 1) {
                // oemId
                map.put("oemId", userBusiEntity.getOemID());
                // oemCode
                map.put("oemCode", userBusiEntity.getOemCode());
                // groupId
                map.put("groupId", userBusiEntity.getGroupID());
                // groupCode
                map.put("groupCode", userBusiEntity.getGroupCode());
                // 创建人名称
                map.put("createdName", userBusiEntity.getEmpName());
                // 修改人名称
                map.put("modifyName", userBusiEntity.getEmpName());
                // 最後修改人 modifier
                map.put("modifier", userBusiEntity.getUserID());
                // 创建人
                map.put("creator", userBusiEntity.getUserID());
                // 并发控制Id updateDateControlId
                map.put("updateControlId", IdWorker.get32UUID());
                // 创建时间 createdDate
                map.put("createdDate", LocalDateTime.now());
                // 最後更新時間 lastUpdateDate
                map.put("lastUpdatedDate", LocalDateTime.now());
            } else {
                // 修改
                // 最後更新時間 lastUpdateDate
                map.put("lastUpdatedDate", LocalDateTime.now());
                // 最後修改人 modifier
                map.put("modifier", userBusiEntity.getUserID());
                // 最後修改人名称 modifierName
                map.put("modifyName", userBusiEntity.getEmpName());
                // 并发控制Id updateDateControlId
                map.put("updateControlId", IdWorker.get32UUID());
            }
            return object;
        }
        Class<T> clazz = (Class<T>) object.getClass();
        try {
            // 保存
            if (isSave == 1) {
                // oemId
                Method method = clazz.getMethod("setOemId", String.class);
                method.invoke(object, userBusiEntity.getOemID());
                // oemCode
                Method method1 = clazz.getMethod("setOemCode", String.class);
                method1.invoke(object, userBusiEntity.getOemCode());
                // groupId
                Method method2 = clazz.getMethod("setGroupId", String.class);
                method2.invoke(object, userBusiEntity.getGroupID());
                // groupCode
                Method method3 = clazz.getMethod("setGroupCode", String.class);
                method3.invoke(object, userBusiEntity.getGroupCode());
                // 创建人名称
                Method method4 = clazz.getMethod("setCreatedName", String.class);
                method4.invoke(object, userBusiEntity.getEmpName());
                // 修改人名称
                Method method5 = clazz.getMethod("setModifyName", String.class);
                method5.invoke(object, userBusiEntity.getEmpName());
                // 最後修改人 modifier
                Method method6 = clazz.getMethod("setModifier", String.class);
                method6.invoke(object, userBusiEntity.getUserID());
                // 创建人
                Method method7 = clazz.getMethod("setCreator", String.class);
                method7.invoke(object, userBusiEntity.getUserID());
                // 并发控制Id updateDateControlId
                Method method8 = clazz.getMethod("setUpdateControlId", String.class);
                method8.invoke(object, IdWorker.get32UUID());
                // 创建时间 createdDate
                Method method9 = clazz.getMethod("setCreatedDate", LocalDateTime.class);
                method9.invoke(object, LocalDateTime.now());
                // 最後更新時間 lastUpdateDate
                Method method10 = clazz.getMethod("setLastUpdatedDate", LocalDateTime.class);
                method10.invoke(object, LocalDateTime.now());
            } else {
                // 修改
                // 最後更新時間 lastUpdateDate
                Method method = clazz.getMethod("setLastUpdatedDate", LocalDateTime.class);
                method.invoke(object, LocalDateTime.now());
                // 最後修改人 modifier
                Method method1 = clazz.getMethod("setModifier", String.class);
                method1.invoke(object, userBusiEntity.getUserID());
                // 最後修改人名称 modifierName
                Method method2 = clazz.getMethod("setModifyName", String.class);
                method2.invoke(object, userBusiEntity.getEmpName());
                // 并发控制Id updateDateControlId
                Method method7 = clazz.getMethod("setUpdateControlId", String.class);
                method7.invoke(object, IdWorker.get32UUID());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return object;
    }
    
    /**
     * 
     * @Description: 针对job没有对应token时插入默认值
     * @version: v1.0.0
     * @author: ly-mengs
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @date: 2019年12月16日 19:59:09
     *
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------* 2019年12月16日
     *        ly-mengs v1.0.0 修改原因
     */
    @SuppressWarnings("unchecked")
    public static <T> T invokeUserInfo(T object) {
        if (object == null) {
            return null;
        }
        Class<T> clazz = (Class<T>) object.getClass();
        try {
            // oemId
            Method method = clazz.getMethod("setOemId", String.class);
            method.invoke(object, "1");
        } catch (Exception e) {
        }
        try {
            // oemCode
            Method method1 = clazz.getMethod("setOemCode", String.class);
            method1.invoke(object, "1");
        } catch (Exception e) {
        }
        try {
            // groupId
            Method method2 = clazz.getMethod("setGroupId", String.class);
            method2.invoke(object, "1");
        } catch (Exception e) {
        }
        try {
            // groupCode
            Method method3 = clazz.getMethod("setGroupCode", String.class);
            method3.invoke(object, "1");
        } catch (Exception e) {
        }
        try {
            // 创建人名称
            Method method4 = clazz.getMethod("setCreatedName", String.class);
            method4.invoke(object, "SYS");
        } catch (Exception e) {
        }
        try {
            // 修改人名称
            Method method5 = clazz.getMethod("setModifyName", String.class);
            method5.invoke(object, "SYS");
        } catch (Exception e) {
        }
        try {
            // 最後修改人 modifier
            Method method6 = clazz.getMethod("setModifier", String.class);
            method6.invoke(object, "SYS");
        } catch (Exception e) {
        }
        try {
            // 创建人
            Method method7 = clazz.getMethod("setCreator", String.class);
            method7.invoke(object, "SYS");
        } catch (Exception e) {
        }
        try {
            // 并发控制Id updateDateControlId
            Method method8 = clazz.getMethod("setUpdateControlId", String.class);
            method8.invoke(object, UUID.randomUUID().toString());
        } catch (Exception e) {
        }
        try {
            // 创建时间 createdDate
            Method method9 = clazz.getMethod("setCreatedDate", LocalDateTime.class);
            method9.invoke(object, LocalDateTime.now());
        } catch (Exception e) {
        }
        try {
            // 最後更新時間 lastUpdateDate
            Method method10 = clazz.getMethod("setLastUpdatedDate", LocalDateTime.class);
            method10.invoke(object, LocalDateTime.now());
        } catch (Exception e) {
        }
        try {
            // IsEnable
            Method method11 = clazz.getMethod("setIsEnable", String.class);
            method11.invoke(object, "1");
        } catch (Exception e) {
        }
        try {
            // SdpOrgId
            Method method12 = clazz.getMethod("setSdpOrgId", String.class);
            method12.invoke(object, "2");
        } catch (Exception e) {
        }
        try {
            // SdpUserId
            Method method13 = clazz.getMethod("setSdpUserId", String.class);
            method13.invoke(object, "88888");
        } catch (Exception e) {
        }
        try {
            // IsSystem
            Method method14 = clazz.getMethod("setIsSystem", String.class);
            method14.invoke(object, "1");
        } catch (Exception e) {
        }
        return object;
    }

    /**
     * 
     * Copyright: Copyright (c) 2019 ly-tianh
     * 
     * @Description: eap实体赋值
     * @version: v1.0.0
     * @author: ly-tianh
     * @date: 2019年7月24日 上午10:09:23 Modification History: Date Author Version
     *        Description ---------------------------------------------------------*
     *        2019年7月24日 ly-tianh v1.0.0 修改原因
     */
    @SuppressWarnings("unchecked")
    public static <T> T invokeEapUserInfo(T object, int isSave) {
        if (object == null) {
            return null;
        }
        UserEntity userEntity = BusicenContext.getCurrentUser();
        if (userEntity == null) {
            userEntity = new UserEntity();
            userEntity.setUserID("1");
        }
        Class<T> clazz = (Class<T>) object.getClass();
        try {
            // 保存
            if (isSave == 1) {
                // 最後修改人 modifier
                Method method6 = clazz.getMethod("setLastUpdatedBy", String.class);
                method6.invoke(object, userEntity.getUserID());
                // 创建人
                Method method7 = clazz.getMethod("setCreatedBy", String.class);
                method7.invoke(object, userEntity.getUserID());
                // 创建时间 createdDate
                Method method9 = clazz.getMethod("setCreatedTime", LocalDateTime.class);
                method9.invoke(object, LocalDateTime.now());
                // 最後更新時間 lastUpdateDate
                Method method10 = clazz.getMethod("setLastUpdatedTime", LocalDateTime.class);
                method10.invoke(object, LocalDateTime.now());
            } else {
                // 修改
                // 最後更新時間 lastUpdateDate
                Method method = clazz.getMethod("setLastUpdatedTime", LocalDateTime.class);
                method.invoke(object, LocalDateTime.now());
                // 最後修改人 modifier
                Method method1 = clazz.getMethod("setLastUpdatedBy", String.class);
                method1.invoke(object, userEntity.getUserID());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return object;
    }

    /**
     * 
     * @Description: 扩展字段的分页查询
     *
     * @version: v1.0.0
     * @author: ly-wyifan
     * @date: 2019年7月14日 下午4:27:15
     *
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------* 2019年7月14日
     *        ly-wyifan v1.0.0 修改原因
     */
    public static <T> ListResult<T> convert2Dynamic(Class<T> clazz, ListResult<?> listResult) {
        if (listResult == null) {
            return null;
        }
        ListResult<T> result = new ListResult<>();
        List<T> convertList = BusicenConvert.convertList(clazz, listResult.getRows());
        BeanUtils.copyProperties(listResult, result, "rows");
        result.setRows(convertList);
        if ("0".equals(listResult.getResult())) {
            result.setMsg(listResult.getMsg());
        } else {
            result.setResult("1");
            result.setMsg("查询成功");
        }
        return result;
    }

    /**
     * 
     * @Description: 扩展字段的单条查询
     *
     * @version: v1.0.0
     * @author: 尹加豪
     * @date: 2019年11月25日
     */
    public static <T> EntityResult<T> convert2Dynamic(Class<T> clazz, EntityResult<?> entityResult) {
        if (entityResult == null) {
            return null;
        }
        EntityResult<T> result = new EntityResult<>();
        T convertObj = BusicenConvert.convert(clazz, entityResult.getRows());
        BeanUtils.copyProperties(entityResult, result, "rows");
        result.setRows(convertObj);
//      if ("0".equals(entityResult.getResult())) {
//          result.setMsg(entityResult.getMsg());
//      } else {
//          result.setResult("1");
//          result.setMsg("查询成功");
//      }
        return result;
    }
    
    /**
     * 
     * @Description: List 转 String
     *
     * @version: v1.0.0
     * @author: ly-wyifan
     * @date: 2019年7月16日 下午8:14:40
     *
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------* 2019年7月16日
     *        ly-wyifan v1.0.0 修改原因
     */
    public static String listToString(List<String> mList) {
        String convertedListStr = "";
        if (null != mList && mList.size() > 0) {
            String[] mListArray = mList.toArray(new String[mList.size()]);
            for (int i = 0; i < mListArray.length; i++) {
                if (i < mListArray.length - 1) {
                    convertedListStr += mListArray[i] + ",";
                } else {
                    convertedListStr += mListArray[i];
                }
            }
            return convertedListStr;
        } else
            return null;
    }

    /**
     * 
     * @Description: 驼峰转下划线
     *
     * @version: v1.0.0
     * @author: ly-wyifan
     * @date: 2019年7月19日 下午2:00:43
     *
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------* 2019年7月19日
     *        ly-wyifan v1.0.0 修改原因
     */
    public static String HumpToUnderline(String para) {
        StringBuilder sb = new StringBuilder(para);
        int temp = 0;// 定位
        if (!para.contains("_")) {
            for (int i = 0; i < para.length(); i++) {
                if (Character.isUpperCase(para.charAt(i))) {
                    sb.insert(i + temp, "_");
                    temp += 1;
                }
            }
        }
        return sb.toString().toUpperCase();
    }
    /**
     * 
     * @Description: 下划线转驼峰命名
     * @author ly-tianh
     * @date 2019年8月29日
     */
    public static String UnderLineToHump(String param)
    {
          StringBuilder sb = new StringBuilder();

          boolean nextUpperCase = false;
          for (int i = 0; i < param.length(); i++) {
              char c = param.charAt(i);
              if (c == '_') {
                  if (sb.length() > 0) {
                      nextUpperCase = true;
                  }
              } else {
                  if (nextUpperCase) {
                      sb.append(Character.toUpperCase(c));
                      nextUpperCase = false;
                  } else {
                      sb.append(Character.toLowerCase(c));
                  }
              }
          }
          return sb.toString();
    }
    
    /**
     * 
     * @Description: 下划线转驼峰命名
     * @author ly-mengs
     * @date 2019年12月26日
     */
    public static Map<String, Object> UnderLineToHump(Map<String, Object> paramMap) {
        Map<String, Object> newMap = new HashMap<>();
        Iterator<Map.Entry<String, Object>> it = paramMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = it.next();
            String key = entry.getKey();
            String newKey = UnderLineToHump(key);
            newMap.put(newKey, entry.getValue());
        }
        return newMap;
    }
    /**
     * 
     * @Description: Manggo里指定返回字段时获取主表所有字段
     *
     * @version: v1.0.0
     * @author: ly-wyifan
     * @date: 2019年7月19日 下午2:01:03
     *
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------* 2019年7月19日
     *        ly-wyifan v1.0.0 修改原因
     */
    public static <T> String getEntityFields(T info) {
        Field[] fields = info.getClass().getDeclaredFields();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < fields.length; i++) {
            if ("serialVersionUID".contentEquals(fields[i].getName())) {
                continue;
            }
            buffer.append(HumpToUnderline("\"" + fields[i].getName()) + "\",");
        }
        buffer.deleteCharAt(buffer.length() - 1);
        return buffer.toString();
    }

    @SuppressWarnings("unchecked")
    public static <T> T getCodeForCheckRehedit(T object) {
        if (Objects.isNull(object)) {
            return null;
        }
        UserBusiEntity userBusiEntity = BusicenContext.getCurrentUserBusiInfo();
        Class<T> clazz = (Class<T>) object.getClass();
        Method method1;
        try {
            // 获得OEM_CODE
            method1 = clazz.getMethod("setOemCode", String.class);
            method1.invoke(object, userBusiEntity.getOemCode());
            // 获得GROUP_CODE
            Method method3 = clazz.getMethod("setGroupCode", String.class);
            method3.invoke(object, userBusiEntity.getGroupCode());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return object;
    }

    /**
     * 
     * @Description: Map 转 实体
     *
     * @version: v1.0.0
     * @author: ly-wyifan
     * @date: 2019年7月25日 下午5:08:12
     *
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------* 2019年7月25日
     *        ly-wyifan v1.0.0 修改原因
     */
    private static final String FORMATER = "yyyy-MM-dd HH:mm:ss";
//  private static final String FORMATER_T = "yyyy-MM-ddTHH:mm:ss";
    private static final String FORMATER_T = "yyyy-MM-dd['T'HH:mm:ss[Z]]";

    public static <T> T map2Object(Map<String, Object> map, Class<T> clazz) {
        if (map == null) {
            return null;
        }
        T obj = null;
        try {
            obj = clazz.newInstance();
            
            for (Class<?> clazzz = clazz; !clazzz.equals(Object.class); clazzz = clazzz.getSuperclass()) {
                Field[] fields = clazzz.getDeclaredFields();
                for (Field field : fields) {
                    Object value = map.get(field.getName());
                    if (value == null) {
                        continue;
                    }
                    int mod = field.getModifiers();
                    if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                        continue;
                    }
                    field.setAccessible(true);
                    if (field.getType().equals(LocalDateTime.class)) {
                        LocalDateTime temp = null;
                        if (value instanceof LocalDateTime) {
                            temp = (LocalDateTime) value;
                        } else if (value instanceof String) {
                            if (StringUtils.isNotBlank(value.toString())) {
                                try {
                                    if (value.toString().contains("T")) {
                                        temp = LocalDateTime.parse(value.toString(),
                                                DateTimeFormatter.ofPattern(FORMATER_T));
                                    } else {
                                        temp = LocalDateTime.parse(value.toString(), DateTimeFormatter.ofPattern(FORMATER));
                                    }

                                } catch (Exception e) {

                                }
                            }
                        } else if (value instanceof Long) {
                            try {
                                Instant instant = Instant.ofEpochMilli((Long) value);
                                ZoneId zone = ZoneId.systemDefault();
                                temp = LocalDateTime.ofInstant(instant, zone);
                            } catch (Exception e) {

                            }
                        } else if (value instanceof Integer) {
                            try {
                                Long timestamp = ((Integer) value).longValue() * 1000;
                                Instant instant = Instant.ofEpochMilli(timestamp);
                                ZoneId zone = ZoneId.systemDefault();
                                temp = LocalDateTime.ofInstant(instant, zone);
                            } catch (Exception e) {

                            }
                        } else if (value instanceof Timestamp) {
                            temp = ((Timestamp) value).toLocalDateTime();
                        }

                        field.set(obj, temp);
                        continue;

                    }

                    if (field.getType().equals(BigDecimal.class)) {
                        String bigDecimal = String.valueOf(map.get(field.getName()));
                        if (bigDecimal.equalsIgnoreCase("null")) {
                            field.set(obj, null);
                        } else {
                            field.set(obj, new BigDecimal(bigDecimal));
                        }
                        continue;
                    }
                    
                    if (field.getType().equals(Integer.class)) {
                        String integer = String.valueOf(map.get(field.getName()));
                        if (integer.equalsIgnoreCase("null")) {
                            field.set(obj, null);
                        } else {
                            field.set(obj, new Integer(integer));
                        }
                        continue;
                    }
                    
                    field.set(obj, map.get(field.getName()));

                }
            }

            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }

    public static ListToStr list2Str() {
        return new ListToStr();
    }

    public static go strToList() {
        return new go();
    }

    public static class go {

        List<String> list = new ArrayList<String>();

        @SuppressWarnings("null")
        public go add(String... str) {
            if (str == null) {
                return this;
            }
            for (String string : str) {
                list.add(string);
            }

            return this;
        }

        public Map<String, Object> build(Map<String, Object> mapParam) {
            if (list.size() == 0) {
                return mapParam;
            }
            for (String str : list) {
                if (!StringHelper.IsEmptyOrNull(mapParam.get(str))) {
                    mapParam.put(str, Arrays.asList(mapParam.get(str).toString().split(",")));
                } else {
                    mapParam.put(str, new ArrayList<String>());
                }
            }
            return mapParam;
        }

    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static <T> T oemGroupforQuery(T info) {
        String oemCode = BusicenContext.getCurrentUserBusiInfo().getOemCode();
        String groupCode = BusicenContext.getCurrentUserBusiInfo().getGroupCode();
        Class clazz = info.getClass();
        try {
            if (info instanceof Map ) {
                if (StringHelper.IsEmptyOrNull(((Map) info).get("oemCode"))) {
                    ((Map) info).put("oemCode", oemCode);
                }
                if (StringHelper.IsEmptyOrNull(((Map) info).get("groupCode"))) {
                    ((Map) info).put("groupCode", groupCode);
                }
            }else {
                Field oemCodeField = clazz.getDeclaredField("oemCode");
                Field groupCodeField = clazz.getDeclaredField("groupCode");
                oemCodeField.setAccessible(true);
                groupCodeField.setAccessible(true);
                if (StringHelper.IsEmptyOrNull(oemCodeField.get(info))) {
                    Method method = clazz.getMethod("setOemCode", String.class);
                    method.invoke(info, oemCode);
                }
                if (StringHelper.IsEmptyOrNull(groupCodeField.get(info))) {
                    Method method1 = clazz.getMethod("setGroupCode", String.class);
                    method1.invoke(info, groupCode);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
        
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static <T> T oemGroupDlrforQuery(T info) {
        String oemCode = BusicenContext.getCurrentUserBusiInfo().getOemCode();
        String groupCode = BusicenContext.getCurrentUserBusiInfo().getGroupCode();
        String dlrId = BusicenContext.getCurrentUserBusiInfo().getDlrID();
        Class clazz = info.getClass();
        try {
            if (info instanceof Map ) {
                if (StringHelper.IsEmptyOrNull(((Map) info).get("oemCode"))) {
                    ((Map) info).put("oemCode", oemCode);
                }
                if (StringHelper.IsEmptyOrNull(((Map) info).get("groupCode"))) {
                    ((Map) info).put("groupCode", groupCode);
                }
                if (StringHelper.IsEmptyOrNull(((Map) info).get("dlrId"))) {
                    ((Map) info).put("dlrId", dlrId);
                }
            }else {
                Field oemCodeField = clazz.getDeclaredField("oemCode");
                Field groupCodeField = clazz.getDeclaredField("groupCode");
                oemCodeField.setAccessible(true);
                groupCodeField.setAccessible(true);
                if (StringHelper.IsEmptyOrNull(oemCodeField.get(info))) {
                    Method method = clazz.getMethod("setOemCode", String.class);
                    method.invoke(info, oemCode);
                }
                if (StringHelper.IsEmptyOrNull(groupCodeField.get(info))) {
                    Method method1 = clazz.getMethod("setGroupCode", String.class);
                    method1.invoke(info, groupCode);
                }
                Field[] fields = clazz.getDeclaredFields();
                for (int i = 0; i < fields.length; i++) {
                    Field f = fields[i];
                    if (f.getName().equals("dlrId")) {
                        f.setAccessible(true);
                        if (StringHelper.IsEmptyOrNull(f.get(info))) {
                            Method method1 = clazz.getMethod("setDlrId", String.class);
                            method1.invoke(info, dlrId);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
        
    }
    
    public static Map<String, Object> entityToMap(Object entity) {
        if (entity == null) {
            return null;
        }
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(entity.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor m : propertyDescriptors) {
                String name = m.getName();
                if (name.equals("callback")||name.equals("callbacks")||name.equals("class")) {
                    continue;
                }
                Method method = m.getReadMethod();
                result.put(name, method.invoke(entity));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }
    
    /**
     * 获取accessToken
     * @author kxq
     * @version 2020年6月9日 下午2:00:50
     * 
     */
    
    public static String getAccessToken() {
        String accessToken = "";
        String saveJason = "";
        
        try {
            Long currenttime = System.currentTimeMillis();
            URL url = new URL("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxa29f9b6dc75ece95&secret=7f17e12d415870a0c84734344d79cb1b");
            HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
            urlcon.connect(); // 获取连接
            InputStream is = urlcon.getInputStream();
            BufferedReader buffer = new BufferedReader(new InputStreamReader(is));
            StringBuffer bs = new StringBuffer();
            String str = null;
            while ((str = buffer.readLine()) != null) {
                bs.append(str);
            }
            JSONObject json = JSONObject.parse(bs.toString());
            System.out.println("------------" + json.getString("access_token"));
            accessToken = json.getString("access_token");
            Long currenttime2 = System.currentTimeMillis();
            if((currenttime2-currenttime)/1000/60/60>1) {
                  
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return accessToken;
    }


    public static void main(String[] args) {
        getAccessToken();
    }
    
    public static class ListToStr {

        List<String> list = new ArrayList<String>();
         String spiletor = ",";
       
       public ListToStr add(String str) {
           list.add(str);
           return this;
       }
       
       public ListToStr spiltor(String sp) {
           spiletor = sp;
           return this;
       }
       
       @Override
       public String toString() {
           StringBuffer buffer = new StringBuffer();
           if (CollectionUtils.isEmpty(list)) {
               return "";
           }
           if (list.size() == 1 && "".equals(list.get(0))) {
               return "";
           }
           for (String string : list) {
               buffer.append(string+spiletor);
           }
           String substring = buffer.toString().substring(0,buffer.length()-1);
           return substring;
       }
   }
}
